<?php
namespace app\admin\controller;
use app\admin\model\Admin_model;
use think\Controller;
use think\Db;
use think\Request;

Class Agentuser extends Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('active','首页');
            $this->assign('fun',$fun);
        }else{
            $this->redirect('index/login/index');
        }
    }
    public function index(){
        $amount=$this->get_user_amount();
        $this->assign('amount',$amount);
        $this->assign('title','首页');
        return $this->fetch();
    }
    //查询用户余额
    public function  get_user_amount(){
        $user=session('user');
        $amout=Db::name('user_balance')->where('user_id',$user['id'])->find();
        if(empty($amout)){
            $am['username']=$user['username'];
            $am['user_id']=$user['id'];
            $id=Db::name('user_balance')->insertGetId($am);
            if($id){
                $amout=Db::name('user_balance')->where('user_id',$user['id'])->find();
                return $amout;
            }else{
                return array();
            }
        }else{
            return $amout;
        }
    }
    //

}